package MovieGuessingGame.login;

import javax.net.ssl.SSLSocket;

import org.apache.commons.codec.binary.Base64;

import MovieGuessingGame.Database.PlayerDB;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

class ServerSSLWorker extends Thread {

	BufferedReader input = null;
	PrintWriter output = null;
	SSLSocket sslsocket = null;
	String userName = null;
	String password = null;
	String dbPass	= null;

	/** REMOVED SO THAT DBPASS PASSED IS USED ***
	 
	 public ServerSSLWorker(ServerSSL sslServer, SSLSocket socket) {
		sslsocket = socket;
		System.out.println("ServerSSLWorker");
		
	}
	**************************************************************/
	
	public ServerSSLWorker(ServerSSL sslServer, SSLSocket socket, String dbPass) {
		sslsocket = socket;
		this.dbPass = dbPass;
		System.out.println("ServerSSLWorker - with dbPass");
	}

	void init() throws IOException {
		input = new BufferedReader(new InputStreamReader(sslsocket.getInputStream()));
		output = new PrintWriter(sslsocket.getOutputStream(), true);		
	}

	void answer(boolean valid) throws IOException {

		String answerString = null;
		if (valid) {
			try {
				byte[] hash =  getHash(userName);
				answerString = new String( Base64.encodeBase64(hash) );
				PlayerDB playerdb = new PlayerDB(dbPass);
				playerdb.insertAndUpdatePlayerRecord(true, userName, hash, 0, 0, false); 
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				answerString = "try again later";
			}

		} else {
			answerString = "you are not authorized";
		}
		
		output.println(answerString);
		System.out.println("wrote " + answerString);

	}

	void getUserPassword() throws IOException {
		String string = null;
		boolean ok = true;
		try {
			string = input.readLine();
		} catch (Exception e) {
			ok = false;
		}
		if (ok) {
			System.out.println(string);
			int index = string.indexOf(",");
			userName = string.substring(0, index);
			password = string.substring(index + 1);
			System.out.println(userName + "-" + password);
		}
	}

	boolean verifyUser() {
		LoginDB logindn = new LoginDB();
		boolean valid = logindn.validate(userName, password, dbPass);
		return valid;
	}

	public byte[] getHash(String string) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		MessageDigest digest = MessageDigest.getInstance("SHA-256");
		digest.reset();
		byte[] bytesArray = digest.digest(string.getBytes("UTF-8"));

		return bytesArray;
	}

	/** * The run method of this thread. */
	public void run() {
		try {
			init();
			getUserPassword();
			boolean valid = verifyUser();
			answer(valid);
		} catch (IOException e) {
			System.out.println("Player died: " + e);
		} finally {
			try {
				sslsocket.close();
			} catch (IOException e) {
			}
		}
	}

	public static void main(String[] arstring) {

	}
}